package com.amar.test.testactivityintent;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Lista {  
	public static final String LISTA_TABLE    = "lista";
	public static final String ID             = "_id";
	public static final String LISTA_NAME_KEY = "name";
	private static      String TAG            = "LISTA";
	public static       String LISTA_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "  
													+ Lista.LISTA_TABLE + " (" 
													+ Lista.ID   + " integer primary key autoincrement, "
													+ Lista.LISTA_NAME_KEY  + " text not null);"
													;
	/**
     * metodo insert tabella Lista
     * @param String listaName
     * @return long _id 
     * @exception SQLiteException
     */
	public static long insertLista(SQLiteDatabase myDb,String listaName) {
		try {
  			 ContentValues cv=new ContentValues();
		     cv.put(Lista.LISTA_NAME_KEY, listaName);
		     return myDb.insertOrThrow(Lista.LISTA_TABLE
				   	                 , null
					                 , cv
					                  ) ;			                 
		   } catch (SQLiteException ex) {
			   Log.d(TAG, "Errore inserimento Lista :" + String.valueOf(ex.getMessage()));
		       return 0;
		    } 
	}
	
	/**
     * metodo update tabella Lista
     * @param long listaId
     * @param String listaName
     * @return int numero di righe aggiornate
     * @exception SQLiteException
     */
	public static int updateLista(SQLiteDatabase myDb,long listaId, String listaName){
		try {
			 ContentValues cv=new ContentValues();
			 cv.put(Lista.LISTA_NAME_KEY, listaName);
		     return myDb.update(Lista.LISTA_TABLE
		        	          , cv
		    		          , Lista.ID + "=" + listaId
		    		          , null
		                       );
		   } catch (SQLiteException ex) {
			   Log.d(TAG, "Errore aggiornamento Lista :" + String.valueOf(ex.getMessage()));
			   return 0;
	       } 
	}
	
	/**
     * metodo delete tabella Lista
     * @param long listaId
     * @return int numero di righe cancellate
     * @exception SQLiteException
     */
	public static int deleteLista(SQLiteDatabase myDb,long listaId){
		try {
		     return myDb.delete(Lista.LISTA_TABLE
		    		          , Lista.ID + "=" + listaId
		    		          , null
		                       );
		   } catch (SQLiteException ex) {
			   Log.d(TAG, "Errore eliminazione Lista :" + String.valueOf(ex.getMessage()));
			   return 0;
	       } 
	}
	public static Cursor fetchListaALL(SQLiteDatabase myDb){ 
		return myDb.query(Lista.LISTA_TABLE //tabella
				         , null             //colonna
				         , null             //where condition
				         , null             //??
				         , null             //group by
				         , null             //having
				         , null             //order by
				         );				       	
	}
	public static Cursor IdFetchLista(SQLiteDatabase myDb, long listaId){ 
		return myDb.query(Lista.LISTA_TABLE //tabella
				         , null             //colonna
				         , Lista.ID + "=" + listaId             //where condition
				         , null             //??
				         , null             //group by
				         , null             //having
				         , null             //order by
				         );				       	
	}
	public static Cursor NameFetchLista(SQLiteDatabase myDb, String listaName){ 
		return myDb.query(Lista.LISTA_TABLE //tabella
				         , null             //colonna
				         , Lista.LISTA_NAME_KEY + "='" + listaName +"'"             //where condition
				         , null             //??
				         , null             //group by
				         , null             //having
				         , null             //order by
				         );				       	
	}
}